# -*- coding: utf-8 -*-
"""
Created on Thu Jul 27 09:01:03 2023

@author: skunk69
"""

def is_chinese(char):
    if '\u4e00' <= char <= '\u9fff':
        return True
    else:
        return False

def chinese_align(text, width):
    count = 0
    result = ''
    for char in text:
        count += 2 if is_chinese(char) else 1
        if count <= width:
            result += char
        else:
            result += ' '
            count = 2 if is_chinese(char) else 1
            result += char
    result += (width - count) * ' '
    return result

def space_align(text_list):
    length = max([len(text) for text in text_list])
    result = [text.ljust(length) for text in text_list]
    return result

def count_char(check_str):
    """
    count the numbers of hanzi in check_str.

    Parameters
    ----------
    check_str : string
        checked string.

    Returns
    -------
    TYPE
        DESCRIPTION.

    """
    return sum((1 for ch in check_str \
                if u'\u4e00' <= ch <= u'\u9fff' or \
                    u'\uff01' <= ch <= u'\uff5e' or \
                        u'\u3000' <= ch <= u'\u303F'))

if __name__ == '__main__':
    text = '中文，Chinese'
    print(chinese_align(text, 15))

    text_list = ['Python', 'Java', 'C++','中文']
    print(space_align(text_list))
    
    longstr = """：山有峰顶，海有彼岸。漫漫长途，终有回转。余味苦涩，终有回甘。"""
    print(count_char(longstr))
    